//
//  AddFriendViewController.m
//  IMDome
//
//  Created by 崔志伟 on 2020/8/22.
//  Copyright © 2020 czw. All rights reserved.
//

#import "AddFriendViewController.h"
#import "AddFriendTableViewCell.h"
#import "UserModel.h"

@interface AddFriendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray  * dataAarry ;
@end

@implementation AddFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加好友";
    [self.tableView registerNib:@"AddFriendTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self laodData];
}

- (void)laodData{
     NSMutableDictionary *params  = [NSMutableDictionary dictionary];
     [BaseServiceManager getRequest:@"user/getAllUser" params:params startBlock:^{
         [self showRequestHUDView];
      } successBlock:^(ResponseBaseModel * _Nonnull responseModel) {
          [self hideRequestHUDView];
          self.dataAarry = [UserModel mj_objectArrayWithKeyValuesArray:responseModel.data];
          [self.tableView reloadData];
      } failureBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
          [self hideRequestHUDView];
      }];
    
}
#pragma mark == UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return  self.dataAarry.count;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AddFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddFriendTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
   UserModel *model =  self.dataAarry[indexPath.row];
    cell.data = model;
    cell.addFriendBlock = ^{
        [self addFriend:model.userId];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

- (void)addFriend:(NSString *)friendId{
    NSMutableDictionary *params  = [NSMutableDictionary dictionary];
    [params setObject:[APPUserDataManager shareInstance].userId forKey:@"userId"];
    [params setObject:friendId forKey:@"friendId"];
    [BaseServiceManager postRequest:@"friend/addFriend" params:params startBlock:^{
        [self showRequestHUDView];
    } successBlock:^(ResponseBaseModel * _Nonnull responseModel) {
        [self hideRequestHUDView];
        [self.navigationController popViewControllerAnimated:YES];
    } failureBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
        [self hideRequestHUDView];
    }];
}


@end
